package library.and.xps.com.step.view.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jude.utils.JUtils;
import com.squareup.okhttp.Request;

import io.realm.Realm;
import library.and.xps.com.step.R;
import library.and.xps.com.step.SPConstant;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.m.model.Login;
import library.and.xps.com.step.m.model.RequestParams;
import library.and.xps.com.step.m.model.User;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.view.base.BaseActivity;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    EditText etPhoneNumber;
    EditText etPassword;
    Button btLogin;
    TextView tvRegister;
    TextView tvFindpass;
    ImageView ivThirdQq;
    ImageView ivThirdWeibo;
    ImageView ivThirdWeixin;
    String LOGIN = "LoginActivity.login";
    final int LOGIN_REQUEST = 0;
    final int LOGIN_SUCCESS = 1;

    @Override
    protected int getLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        return R.layout.activity_login;
    }

    @Override
    protected void setData() {

    }

    @Override
    protected void init() {
        etPhoneNumber = r2v(R.id.et_phoneNumber);
        etPassword = r2v(R.id.et_newpassword);
        btLogin = r2v(R.id.bt_login);
        btLogin.setOnClickListener(this);
        tvRegister = r2v(R.id.tv_register);
        tvRegister.setOnClickListener(this);
        tvFindpass = r2v(R.id.tv_findpass);
        tvFindpass.setOnClickListener(this);
        ivThirdQq = r2v(R.id.iv_third_qq);
        ivThirdWeibo = r2v(R.id.iv_third_weibo);
        ivThirdWeixin = r2v(R.id.iv_third_weixin);

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.bt_login:
                Message message = Message.obtain(bkThreadHandler);
                message.what = LOGIN_REQUEST;
                message.sendToTarget();
                break;
            case R.id.tv_register:
                startActivity(new Intent(this, RegisterActivity.class));
                break;
            case R.id.tv_findpass:
                startActivity(new Intent(this, FindBackPassActivity.class));
                break;
            case R.id.iv_third_qq:

                break;
            case R.id.iv_third_weibo:

                break;
            case R.id.iv_third_weixin:

                break;
        }
    }

    void doLogin() {
        String jsonObject = RequestParams.getNewInstance()
                .setPhone(etPhoneNumber.getText().toString())
                .setPassword(etPassword.getText().toString())
                .getJson().toString();

        OkHttpClientUtils.postJsonStringAsync(this, URLConstants.USER.Login, jsonObject, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
                JUtils.Log("onFailure");
            }

            @Override
            public void onResponse(final ResponceWrapper response) {
                if (HttpResponsHelper.SuccessResponce(response)) {
                    loginSuccess(response.getBody());
                }
            }

            @Override
            public void onStart() {

            }
        }, LOGIN);

    }

    void loginSuccess(final String response) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                if (getUser() == null) {
                    User user = new User();
                    Login res = new Gson().fromJson(response, Login.class);
                    if(res==null){
                        realm.commitTransaction();
                        return;
                    }
                    user.setIco(res.getObj().getIco());
                    user.setNickname(res.getObj().getNickname());
                    user.setPhone(res.getObj().getPhone());
                    user.setSex("1");
                    user.setStature(res.getObj().getStature());
                    user.setWeight(res.getObj().getWeight());
                    user.setToken(res.getObj().getToken());
                    user.setUserid(res.getObj().getUserid()+"");
                    JUtils.getSharedPreference().edit().putString(SPConstant.USER_TOKEN,res.getObj().getToken()+"").commit();
                    user.setStepperday(res.getObj().getGoal()+"");
                    realm.insert(user);
                    startActivity(new Intent(LoginActivity.this, NewMainActivity.class));
                }else {
                    startActivity(new Intent(LoginActivity.this, NewMainActivity.class));
                }
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case LOGIN_REQUEST:
                doLogin();
                break;
            case LOGIN_SUCCESS:
                loginSuccess((String) msg.obj);
                break;
        }

        return false;
    }
}
